** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_D1.smcl", replace

use "$project_path/data/4_regdata/regdata.dta", clear

local variables = "log_gdp_penn log_gdp_madd log_gdp_wb log_gdpc_penn log_gdpc_madd log_gdpc_wb growth_gdp_penn growth_gdp_madd growth_gdp_wb growth_gdpc_penn growth_gdpc_madd growth_gdpc_wb growth_cap_penn growth_cons_penn growth_prod_penn inflation_imf inflation_cp_wb inflation_def_wb unemployment_ilo unemployment_oecd country_risk"
local negative_variables = "inflation_imf inflation_cp_wb inflation_def_wb unemployment_ilo unemployment_oecd country_risk"
foreach v in `negative_variables' {
	cap gen mstd_diff_m1_`v' = -std_diff_m1_`v'
}

foreach v in `variables' {
	if strpos("`negative_variables'", "`v'"){
		rdrobust mstd_diff_m1_`v' runvar, all
	}
	else {
		rdrobust std_diff_m1_`v' runvar, all
	}
}

log close

** Building .tex table

//Generate a temporary dta file with variables description. We use an id variable that follows the order of the local `variables'.
preserve
gen description = ""

local i = 1
foreach v in `variables'{
	
	if "`v'" == "log_gdp_penn" local description = "Log GDP"
	if "`v'" == "log_gdp_madd" local description = "Log GDP"
	if "`v'" == "log_gdp_wb" local description = "Log GDP"
	if "`v'" == "log_gdpc_penn" local description = "Log GDP per capita"
	if "`v'" == "log_gdpc_madd" local description = "Log GDP per capita"
	if "`v'" == "log_gdpc_wb" local description = "Log GDP per capita"
	if "`v'" == "growth_gdp_penn" local description = "GDP growth"
	if "`v'" == "growth_gdp_madd" local description = "GDP growth"
	if "`v'" == "growth_gdp_wb" local description = "GDP growth"
	if "`v'" == "growth_gdpc_penn" local description = "GDP per capita growth"
	if "`v'" == "growth_gdpc_madd" local description = "GDP per capita growth"
	if "`v'" == "growth_gdpc_wb" local description = "GDP per capita growth"
	if "`v'" == "growth_cap_penn" local description = "Capital stock growth"
	if "`v'" == "growth_cons_penn" local description = "Consumption growth"
	if "`v'" == "growth_prod_penn" local description = "TFP growth"
	if "`v'" == "inflation_imf" local description = "(Minus) Inflation (CPI)"
	if "`v'" == "inflation_cp_wb" local description = "(Minus) Inflation (CPI)"
	if "`v'" == "inflation_def_wb" local description = "(Minus) Inflation (GDP deflator)"
	if "`v'" == "unemployment_ilo" local description = "(Minus) Unemployment"
	if "`v'" == "unemployment_oecd" local description = "(Minus) Unemployment"
	if "`v'" == "country_risk" local description = "(Minus) Risk rating"
	
	qui replace description = "`description'" in `i'
	
	local ++i
}
keep description 
drop if description == ""
gen id = _n
tempfile description_dta
save `description_dta'
restore

//Generate a temporary dta file with source variable
preserve
gen source = ""
local i = 1
foreach v in `variables' {
	
	replace source = "``v'[note1]'" in `i'
	local ++i
	
}	
keep source
drop if source == ""
gen id = _n
replace source = substr(source, 9,.)
tempfile source_dta
save `source_dta'
restore

//Prepare matrix of coefficients and other statistics from the regression
local rows : word count `variables'
local nrows `rows'
local ncols 4
mat Coefs = J(`nrows', `ncols', .)


local i = 1
foreach v in `variables' {
	if strpos("`negative_variables'", "`v'"){
		rdrobust mstd_diff_m1_`v' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		loca N_`i': di %6.0f e(N)
		
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''	
		mat Coefs[`i',4] = `N_`i''
		
		local ++i
		
	}
	else {
		rdrobust std_diff_m1_`v' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		loca N_`i': di %6.0f e(N)
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''	
		mat Coefs[`i',4] = `N_`i''
		
		local ++i

	}
}

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 estimates
rename Coefs2 std_err
rename Coefs3 pval
rename Coefs4 N

gen outcome = ""

gen id = _n
order id outcome, first

tostring estimates, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace estimates = estimates + "***" if pval < `3_star'
replace estimates = estimates + "**" if  pval < `2_star' & pval > `3_star'
replace estimates = estimates + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}

replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"

//Attach source variable and description
merge 1:1 id using `source_dta', assert(match) nogen
merge 1:1 id using `description_dta', assert(match) nogen

replace outcome = description

drop id description

label var outcome ""
label var estimates "Est."
label var std_err "SE"
label var pval "p-val"
label var N "N"
label var source "Source"

//Prepare texfile

local title "Effects on additional economic performance variables"
local label_key "tab:econ_variables_baseline"
local output "$project_path/output/appendix_tables/Appendix_Table_D1.tex"
local footnote = "\emph{Notes}: This table reports RD estimates corresponding to equation (\ref{eq:baseline_rdd}) for measures of economic performance, expressed in standard deviation terms. It is continued in Appendix Table \ref{tab:intecon_variables_baseline}"

texsave using "`output'", replace varlabels frag nofix 	location(H) ///
					      align(p{6.8cm} p{1.7cm} p{1.5cm} p{1.5cm} p{1.3cm} p{3.2cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'") 
	

